/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.indexdatasource.service.impl;

import com.pingan.haofang.searchcloud.index.constants.IndexDatasourceStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pingan.haofang.searchcloud.common.domain.BaseDomain;
import com.pingan.haofang.searchcloud.common.domain.IdGetter;
import com.pingan.haofang.searchcloud.index.constants.IndexDataSourceType;
import com.pingan.haofang.searchcloud.indexdatasource.dao.IndexDatasourceMQDao;
import com.pingan.haofang.searchcloud.indexdatasource.dao.IndexDatasourceRpcDao;
import com.pingan.haofang.searchcloud.indexdatasource.dao.IndexDatasourceSqlDao;
import com.pingan.haofang.searchcloud.indexdatasource.domain.IndexDatasourceMQ;
import com.pingan.haofang.searchcloud.indexdatasource.domain.IndexDatasourceRpc;
import com.pingan.haofang.searchcloud.indexdatasource.domain.IndexDatasourceSql;
import com.pingan.haofang.searchcloud.indexdatasource.service.ImportDatasourceFacadeService;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author huhailiang937
 * @date 2017年5月11日下午5:53:01
 * @since 1.0.0
 */
@Service
public class ImportDatasourceFacadeServiceImpl implements ImportDatasourceFacadeService {

    @Autowired
    private IndexDatasourceMQDao mqDao;

    @Autowired
    private IndexDatasourceRpcDao rpcDao;

    @Autowired
    private IndexDatasourceSqlDao sqlDao;


    /* (non-Javadoc)
     * @see com.pingan.haofang.searchcloud.indexdatasource.service.ImportDatasourceFacadeService#updateStatus(java.lang.Long, int, java.lang.Long, com.pingan.haofang.searchcloud.index.constants.IndexDataSourceType)
     */
    @Override
    public void updateStatus(Long id, int status, Long opId, IndexDataSourceType incIndexBuildType) {
        switch (incIndexBuildType) {
            case JSON_RPC:
                rpcDao.updateStatus(id, status, opId);
                break;
            case MQ:
                mqDao.updateStatus(id, status, opId);
                break;
            case SQL:
                sqlDao.updateStatus(id, status, opId);
                break;
            default:
                break;
        }
    }

    /* (non-Javadoc)
     * @see com.pingan.haofang.searchcloud.indexdatasource.service.ImportDatasourceFacadeService#findById(java.lang.Long, int, com.pingan.haofang.searchcloud.index.constants.IndexDataSourceType)
     */
    @SuppressWarnings("unchecked")
    @Override
    public <E extends BaseDomain> E findByIdAndStatus(Long id, int status, IndexDataSourceType incIndexBuildType) {
        switch (incIndexBuildType) {
            case JSON_RPC:
                return (E) rpcDao.findByIdAndStatus(id, status);
            case MQ:
                return (E) mqDao.findByIdAndStatus(id, status);
            case SQL:
                return (E) sqlDao.findByIdAndStatus(id, status);
            default:
                return null;
        }
    }

    /* (non-Javadoc)
     * @see com.pingan.haofang.searchcloud.indexdatasource.service.ImportDatasourceFacadeService#saveAndFlush(com.pingan.haofang.searchcloud.common.domain.BaseDomain)
     */
    @Override
    public IdGetter<Long> saveAndFlush(IdGetter<Long> entity, IndexDataSourceType incIndexBuildType) {
        switch (incIndexBuildType) {
            case JSON_RPC:
                return rpcDao.saveAndFlush((IndexDatasourceRpc) entity);
            case MQ:
                return mqDao.saveAndFlush((IndexDatasourceMQ) entity);
            case SQL:
                return sqlDao.saveAndFlush((IndexDatasourceSql) entity);
            default:
                return null;
        }
    }

    @Override
    public List<? extends BaseDomain> findByIdsAndStatus(Set<Long> idList, IndexDatasourceStatus status,
                                                         IndexDataSourceType indexDataSourceType) {
        switch (indexDataSourceType) {
            case JSON_RPC:
                return rpcDao.findByIdsAndStatus(idList, status.getId());
            case MQ:
                return mqDao.findByIdsAndStatus(idList, status.getId());
            case SQL:
                return sqlDao.findByIdsAndStatus(idList, status.getId());
            default:
                return Collections.emptyList();
        }
    }

}
